//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "ConstantRecoveryModel.h"
using namespace Cephei::QL::Experimental::Credit;
#include <gen/QL/Experimental/Credit/DefaultProbKey.h>
#include <gen/QL/Experimental/Credit/RecoveryRateModel.h>
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Experimental::Credit::CConstantRecoveryModel::CConstantRecoveryModel (Double recovery, Microsoft::FSharp::Core::FSharpOption<QL::Experimental::Credit::SeniorityEnum>^ sen) : CRecoveryRateModel(CConstantRecoveryModel::typeid)
{
    try
    {
#ifdef HANDLE
        _phConstantRecoveryModel = NULL;
#endif
        QuantLib::Real _recovery = (QuantLib::Real)ValueHelper::Convert (recovery); //d
        QuantLib::Seniority _sen = 
            (Microsoft::FSharp::Core::FSharpOption<QL::Experimental::Credit::SeniorityEnum>::IsSome::get (sen) ? (QuantLib::Seniority)sen->Value : QuantLib::Seniority::NoSeniority); //10
        _ppConstantRecoveryModel = new boost::shared_ptr<QuantLib::ConstantRecoveryModel> (new QuantLib::ConstantRecoveryModel ( _recovery,  _sen ));
        SetRecoveryRateModel (boost::dynamic_pointer_cast<QuantLib::RecoveryRateModel> (*_ppConstantRecoveryModel));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Experimental::Credit::CConstantRecoveryModel::CConstantRecoveryModel (boost::shared_ptr<QuantLib::ConstantRecoveryModel>& childNative, Object^ owner) : CRecoveryRateModel(CConstantRecoveryModel::typeid)
{
#ifdef HANDLE
	_phConstantRecoveryModel = NULL;
#endif
	_ppConstantRecoveryModel = &childNative;
    _ppRecoveryRateModel = new boost::shared_ptr<QuantLib::RecoveryRateModel> (boost::dynamic_pointer_cast<QuantLib::RecoveryRateModel> (*_ppConstantRecoveryModel));
}
Cephei::QL::Experimental::Credit::CConstantRecoveryModel::CConstantRecoveryModel (QuantLib::ConstantRecoveryModel& childNative, Object^ owner) : CRecoveryRateModel(CConstantRecoveryModel::typeid)
{
#ifdef HANDLE
	_phConstantRecoveryModel = NULL;
#endif
	_ppConstantRecoveryModel = new boost::shared_ptr<QuantLib::ConstantRecoveryModel> (&childNative);
    _ppRecoveryRateModel = new boost::shared_ptr<QuantLib::RecoveryRateModel> (boost::dynamic_pointer_cast<QuantLib::RecoveryRateModel> (*_ppConstantRecoveryModel));
    _ConstantRecoveryModelOwner = owner;
    _RecoveryRateModelOwner = owner;
}

Cephei::QL::Experimental::Credit::CConstantRecoveryModel::CConstantRecoveryModel (CConstantRecoveryModel^ copy) : CRecoveryRateModel(CConstantRecoveryModel::typeid)
{
#ifdef HANDLE
	_phConstantRecoveryModel = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppConstantRecoveryModel = new boost::shared_ptr<QuantLib::ConstantRecoveryModel> (copy->GetShared());
        _ppRecoveryRateModel = new boost::shared_ptr<QuantLib::RecoveryRateModel> (boost::dynamic_pointer_cast<QuantLib::RecoveryRateModel> (*_ppConstantRecoveryModel));
    }
}
Cephei::QL::Experimental::Credit::CConstantRecoveryModel::CConstantRecoveryModel (PLATFORM::Type^ t) : CRecoveryRateModel(CConstantRecoveryModel::typeid)
{
#ifdef HANDLE
	_phConstantRecoveryModel = NULL;
#endif
	if (!t->IsSubclassOf(CConstantRecoveryModel::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Experimental::Credit::CConstantRecoveryModel::CConstantRecoveryModel (QuantLib::Handle<QuantLib::ConstantRecoveryModel>& childNative, Object^ owner)  : CRecoveryRateModel(CConstantRecoveryModel::typeid)
{
	_phConstantRecoveryModel = &childNative;
	_ppConstantRecoveryModel = &static_cast<boost::shared_ptr<QuantLib::ConstantRecoveryModel>>(childNative.currentLink());
    _ppRecoveryRateModel = new boost::shared_ptr<QuantLib::RecoveryRateModel> (boost::dynamic_pointer_cast<QuantLib::RecoveryRateModel> (*_ppConstantRecoveryModel));
    _ConstantRecoveryModelOwner = owner;
}
Cephei::QL::Experimental::Credit::CConstantRecoveryModel::CConstantRecoveryModel (QuantLib::Handle<QuantLib::ConstantRecoveryModel> childNative)  : CRecoveryRateModel(CConstantRecoveryModel::typeid)
{
	_phConstantRecoveryModel = &childNative;
	_ppConstantRecoveryModel = &static_cast<boost::shared_ptr<QuantLib::ConstantRecoveryModel>>(childNative.currentLink());
    _ppRecoveryRateModel = new boost::shared_ptr<QuantLib::RecoveryRateModel> (boost::dynamic_pointer_cast<QuantLib::RecoveryRateModel> (*_ppConstantRecoveryModel));
}
#endif
#ifdef STRUCT
Cephei::QL::Experimental::Credit::CConstantRecoveryModel::CConstantRecoveryModel (QuantLib::ConstantRecoveryModel childNative)  : CRecoveryRateModel(CConstantRecoveryModel::typeid)
{
#ifdef HANDLE
	_phConstantRecoveryModel = NULL;
#endif
	_ppConstantRecoveryModel = new boost::shared_ptr<QuantLib::ConstantRecoveryModel> (new QuantLib::ConstantRecoveryModel (childNative));
    _ppRecoveryRateModel = new boost::shared_ptr<QuantLib::RecoveryRateModel> (boost::dynamic_pointer_cast<QuantLib::RecoveryRateModel> (*_ppConstantRecoveryModel));
}
#endif

Cephei::QL::Experimental::Credit::CConstantRecoveryModel::~CConstantRecoveryModel ()
{
    if (_ppConstantRecoveryModel != NULL)
    {
	    delete _ppConstantRecoveryModel;
        _ppConstantRecoveryModel = NULL;
    }
}
Cephei::QL::Experimental::Credit::CConstantRecoveryModel::!CConstantRecoveryModel ()
{
    if (_ppConstantRecoveryModel != NULL)
    {
	    delete _ppConstantRecoveryModel;
    }
}
QuantLib::ConstantRecoveryModel& Cephei::QL::Experimental::Credit::CConstantRecoveryModel::GetReference ()
{
    if (_ppConstantRecoveryModel == NULL) throw REFNEW NativeNullException ();
	return **_ppConstantRecoveryModel;
}
boost::shared_ptr<QuantLib::ConstantRecoveryModel>& Cephei::QL::Experimental::Credit::CConstantRecoveryModel::GetShared ()
{
    if (_ppConstantRecoveryModel == NULL) throw REFNEW NativeNullException ();
	return *_ppConstantRecoveryModel;
}
QuantLib::ConstantRecoveryModel* Cephei::QL::Experimental::Credit::CConstantRecoveryModel::GetPointer ()
{
    if (_ppConstantRecoveryModel == NULL) throw REFNEW NativeNullException ();
	return &**_ppConstantRecoveryModel;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::ConstantRecoveryModel>& Cephei::QL::Experimental::Credit::CConstantRecoveryModel::GetHandle ()
{
	if (_phConstantRecoveryModel == NULL)
	{
		_phConstantRecoveryModel = new Handle<QuantLib::ConstantRecoveryModel> (*_ppConstantRecoveryModel);
	}
	return *_phConstantRecoveryModel;
}
#endif
bool Cephei::QL::Experimental::Credit::CConstantRecoveryModel::HasNative () 
{
	return (_ppConstantRecoveryModel != NULL);
}

Boolean Cephei::QL::Experimental::Credit::CConstantRecoveryModel::AppliesToSeniority (QL::Experimental::Credit::SeniorityEnum prm1)
{
    try
    {
        QuantLib::Seniority _prm1 = (QuantLib::Seniority)prm1 ;
    	bool _rv = (bool)(*_ppConstantRecoveryModel)->appliesToSeniority ( _prm1 );   
        Boolean _nrv = (Boolean)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Experimental::Credit::IConstantRecoveryModel^ Cephei::QL::Experimental::Credit::CConstantRecoveryModel::Update::get ()
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
    	(*_ppConstantRecoveryModel)->update ( );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Experimental::Credit::IConstantRecoveryModel^ Cephei::QL::Experimental::Credit::CConstantRecoveryModel_Factory::Create (Double recovery, Microsoft::FSharp::Core::FSharpOption<QL::Experimental::Credit::SeniorityEnum>^ sen)
{
    return REFNEW CConstantRecoveryModel ( recovery,  sen);
}
